package com.icesoft.common.utils.jwt;

import sun.misc.BASE64Decoder;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;

/**
 * @program: icesoft-security
 * @description: JWT 加密类
 * @author: xuefeng.gao
 * @create: 2018-11-06 15:27
 **/
public class DESCoder {

    private static String KEY_ALGORTHM = "DES";
    private Key toKey(byte[] key) throws Exception {
        DESKeySpec dks = new DESKeySpec(key);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORTHM);
        SecretKey secretKey = keyFactory.generateSecret(dks);
        return secretKey;
    }

    /**
     *
     * @param key key(Base64编码）
     * @return
     * @throws Exception
     */
    public Key toKey(String key) throws Exception{
        byte[] keyBytes = (new BASE64Decoder()).decodeBuffer(key);
        Key keyObj = toKey(keyBytes);
        return keyObj;
    }

}
